<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                @click="addRole"
                icon="el-icon-plus"
                size="small"
                type="primary"
                >新增角色</el-button
              >
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="list" v-loading="loading">
              <el-table-column
                type="index"
                align="center"
                label="序号"
                width="120"
              />
              <el-table-column
                prop="name"
                align="center"
                label="角色名称"
                width="240"
              />
              <el-table-column prop="description" align="center" label="描述" />
              <el-table-column align="center" label="操作">
                <template slot-scope="scope">
                  <el-button
                    @click="openrole(scope.row.id)"
                    size="small"
                    type="success"
                    >分配权限</el-button
                  >
                  <el-button
                    @click="opendeit(scope.row)"
                    size="small"
                    type="primary"
                    >编辑</el-button
                  >
                  <el-button
                    size="small"
                    type="danger"
                    @click="deleteRole(scope.row.id)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <!-- 添加样式 -->
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                @size-change="sizechange"
                @current-change="currentchange"
                :current-page="page.page"
                :page-size="page.pagesize"
                :total="page.total"
                layout="total, sizes, prev, pager, next, jumper"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form
              :model="formDatas"
              label-width="120px"
              style="margin-top: 50px"
            >
              <el-form-item label="公司名称">
                <el-input
                  v-model="formDatas.name"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input
                  v-model="formDatas.companyAddress"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="公司电话">
                <el-input
                  v-model="formDatas.companyPhone"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input
                  v-model="formDatas.mailbox"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  type="textarea"
                  :rows="3"
                  disabled
                  style="width: 400px"
                  v-model="formDatas.remarks"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <!-- 编辑弹窗 -->
    <el-dialog title="编辑弹层" :visible="showDialog">
      <el-form
        ref="formData"
        :model="formData"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="formData.description" />
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button @click="submit" type="primary" size="small"
            >确定</el-button
          >
          <el-button @click="cancel" size="small">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
    <!-- 面板：分配权限 -->
    <setrole
      ref="role"
      :id="id"
      :showPermDialog.sync="showPermDialog"
    ></setrole>
  </div>
</template>

<script>
import {
  getRoleList,
  getCompanyInfo,
  deleteRole,
  getRoleDetail,
  updateRole,
  addRole
} from '@/api/setting'
import { mapGetters } from 'vuex'

export default {
  components: {
    setrole: () => import('./components/setrole.vue')
  },
  data () {
    return {
      showDialog: false, //编辑弹窗
      loading: false,
      list: [],
      page: {
        page: 1,
        pagesize: 10,
        total: 0 //记录总数
      },
      // showDialog: false,
      // 定义表单元素中的数据源
      formData: {
        name: '', // 角色名称
        description: '' // 角色描述
      },
      formDatas: {},
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      },
      // 控制分配权限面板的显示与隐藏
      showPermDialog: false,
      // 当前操作的角色 id
      id: ''
    }
  },
  computed: {
    ...mapGetters(['companyId'])
  },
  created () {
    this.getRoleList() //获取角色列表
    this.getCompanyInfo()
  },
  methods: {
    //获取获取角色列表
    getRoleList () {
      this.loading = true
      setTimeout(async () => {
        const res = await getRoleList()
        console.log('角色列表', res)
        const { total, rows } = await getRoleList(this.page)
        //保留数据源
        this.page.total = total
        this.list = rows
        this.loading = false
      }, 50)
    },
    //获取公司的信息
    async getCompanyInfo () {
      this.formDatas = await getCompanyInfo(this.companyId)
      console.log('公司信息', this.formDatas)
    },
    // 根据id删除角色
    async deleteRole (id) {
      try {
        await this.$confirm('确认删除该角色吗')
        await deleteRole(id)
        this.getRoleList()
        this.$message.success('删除角色成功')
      } catch (error) {
        console.log(error)
      }
    },
    addRole () {
      this.showDialog = true
    },
    async submit () {
      try {
        await this.$refs.formData.validate()
        if (this.formData.id) {
          await updateRole(this.formData)
        } else {
          await addRole(this.formData)
          this.formData = {
            name: '', // 角色名称
            description: '' // 角色描述
          }
        }
        this.getRoleList()
        this.$message.success('操作成功')
        this.showDialog = false
      } catch (error) {
        console.log('公司设置页面', error)
      }
    },
    async cancel () {
      this.showDialog = false
      this.formData = {
        name: '', // 角色名称
        description: '' // 角色描述
      }
    },
    // 修改当前页
    currentchange (page) {
      this.page.page = page //当前页码赋值给当前的最新页码
      this.getRoleList()
    },
    //修改页容量
    sizechange (size) {
      this.page.pagesize = size
      this.getRoleList()
    },
    opendeit (obj) {
      this.formData = { ...obj }
      this.showDialog = true
    },
    async openrole (id) {
      this.showPermDialog = true
      // 调用获取角色权限的方法
      await this.$refs.role.getRolePermission(id)
      // 保存当前操作的 id
      this.id = id
    }
  }
}
</script>

<style lang="scss" scoped></style>
